<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/taglib/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/taglib/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/taglib/struts-logic.tld" prefix="logic" %>


<!-- ############################ Validation and dynamic creation of criteria ############################  -->
<script type="text/javascript">
	function addNewCiteria() {
		var name = document.getElementById("criteriaName").value;
		var discr = document.getElementById("criteriaDiscription").value;
		
		if(name != "" && discr != "") {
			var newCriteria = document.createElement("p");
			newCriteria.innerHTML = "<input checked=\"true\" type=\"checkbox\" name=\"markPolicyCriteria\" value=\""+ name + "@" + discr +"\">" + name + "(" + discr + ")";
			document.getElementById("criteria_list").appendChild(newCriteria);	
		}
	}
	
	function isValid() {
		var isValid = true;
		if( document.getElementById("policyName").firstChild.value == "" ) {
			alert("Заполните имя политики!");
			isValid = false;
		}
		else if( document.getElementById("policyDiscription").firstChild.value == "" ) {
			alert("Заполните описание политики!");
			isValid = false;
		}
		else if( !document.getElementById("criteria_list").hasChildNodes() ) {
			alert("Добавте критерии оценки!");
			isValid = false;
		}
		return isValid;
	}
</script>


<!-- ################################# Page content ############################ -->

<div id="content" align="center">
	<p><bean:message key="instructor.mpolicy.message.creation"/></p>
	<html:form action="/instructor/mark_policy" method="post" onsubmit="return isValid();">
		<table>
			<tr>
				<td><bean:message key="instructor.mpolicy.message.sh_name"/></td>
				<td id="policyName"><html:text name="markPolicyForm" property="policyShort" value="" /></td>
			</tr>	
			<tr>
				<td><bean:message key="instructor.mpolicy.message.sh_disc"/>:</td>
				<td id="policyDiscription"><html:textarea name="markPolicyForm" property="policyDescription" cols="20" rows="5" value="" /></td>
			</tr>
			<tr>
				<td colspan="3"><hr/></td>	
			</tr>	
			<tr>
				<td><bean:message key="instructor.mpolicy.message.criteria"/>:</td>	
			</tr>		
			<tr>
				<td id="criteria_list"></td>
			<tr>		
			<tr>		
				<td><input type="text" id="criteriaName" value="<bean:message key="instructor.mpolicy.field.cr_name"/>" onClick="(this.value='')" /></td>
				<td>
					<textarea rows="3" cols="20" id="criteriaDiscription" onClick="(this.value='')"><bean:message key="instructor.mpolicy.field.cr_disc"/></textarea>
				</td>
				<td>
					<html:button property="add" onclick="addNewCiteria()"><bean:message key="instructor.mpolicy.button.add_crit"/></html:button>
				</td>
			</tr>
			<tr>
				<td colspan="3"><hr/></td>	
			</tr>
			<tr align="center">
				<td colspan="3">
					<html:hidden property="operationType" value="create"/>
					<html:submit property="method"><bean:message key="instructor.mpolicy.button.save_mp"/></html:submit>
				</td>
			</tr>
		</table>
	</html:form>
</div>